package wrapper;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.csvreader.CsvReader;
import common.*;

/**
 * @author Or Kaplan
 *
 * float id wrapper (convert mid to long and insert to table)
 */
public class FieldId extends FieldBase {

	/**
	 * @param tsvFieldName
	 * @param tableFieldName
	 * @param isRequired
	 */
	public FieldId(String tsvFieldName, String tableFieldName, boolean isRequired) {
		super(tsvFieldName, tableFieldName, isRequired);
	}

	/* (non-Javadoc)
	 * @see wrapper.IFieldWrapper#insertToStatement(java.sql.PreparedStatement, int, com.csvreader.CsvReader)
	 */
	@Override
	public boolean insertToStatement(PreparedStatement statement, int position,
			CsvReader reader) throws IOException, SQLException {
		String mid = reader.get(this.getTsvFieldName());
		return insertToStatement(statement, position, mid);
	}
	
	/* (non-Javadoc)
	 * @see wrapper.IFieldWrapper#insertToStatement(java.sql.PreparedStatement, int, java.lang.String)
	 */
	@Override
	public boolean insertToStatement(PreparedStatement statement, int position, String value) throws SQLException {
		if (Validate(value))
		{
			long id = Utils.DecodeLongMid(value);
			statement.setLong(position, id);
			return true;
		}
		Logger.GetLogger().Log("invalid value at position " + position + ", " + value);
		return false;
	}

	@Override
	public boolean Validate(String data) {
		return (data != null && data.length() > 3 && data.startsWith("/m/") && data.length() <= (Utils.MaxLength + 3));
	}

}
